#include"../include/head.h"
int update(char *table,char* olddata,char* newdata,char* userId)
{
    MYSQL *conn;
	MYSQL_RES *res;
	MYSQL_ROW row;
	char* server="localhost";
	char* user="chen";
	char* password="123";
	char* database="37chen";

	//char query[200]="update user set username ='";
     char query[200]="update ";
     sprintf(query,"%s %s set %s='%s' where id='%s'",query,table,olddata,newdata,userId);
	//sprintf(query,"%s%s%s",query,argv[1],"' where id=3");
	puts(query);

	int queryResult;

	conn=mysql_init(NULL);

	if(!mysql_real_connect(conn,server,user,password,database,0,NULL,0))
	{
		printf("Error connecting to database:%s\n",mysql_error(conn));
	}
    else
    {
		printf("Connected...\n");
	}

	queryResult = mysql_query(conn, query);
	if(queryResult)
	{
		printf("Error making query:%s\n",mysql_error(conn));
	}
    else
    {
        int ret = mysql_affected_rows(conn);
        if(ret)
        {
            printf("update success\n");
        }
        else
        {
            printf("update fail, mysql_affected_rows:%d\n", ret);
        }
	}
	mysql_close(conn);
	return 0;
}
int cdjudge(char* data)
{
    MYSQL *conn;
	MYSQL_RES *res;
	MYSQL_ROW row;
	char *server = "localhost";
	char *user = "chen";
	char *password = "123";
	char *database = "37chen";//要访问的数据库名称
	char query[300] = "select type from file where filename=('";
    char str[25];
    //sprintf(str,"%d",data);//将传进来的用户Id转成字符串
    strcat(query,data);
    strcat(query,"')");
    //printf("query cd %s\n",query);
    
	unsigned int queryRet;

	//printf("pwd = %s\n",pwd);
	//strcpy(query,"select * from user"); 

    //在输出前先打印查询语句
	//puts(query);

    //初始化
	conn = mysql_init(NULL);
    if(!conn)
    {
        printf("MySQL init failed");
        return -1;
    }

    //连接数据库，看连接是否成功，只有成功才能进行后面的操作
	if(!mysql_real_connect(conn, server, user, password, database, 0, NULL, 0))
	{
		printf("Error connecting to database: %s\n", mysql_error(conn));
		return -1;
	}
    else
    {
		//printf("MySQL Connected...\n");
	}

    //把SQL语句传递给MySQL
	queryRet = mysql_query(conn, query);
	if(queryRet)
	{
		printf("Error making query: %s\n", mysql_error(conn));
	}
    else
    {
    
        //用mysql_num_rows可以得到查询的结果集有几行
        //要配合mysql_store_result使用
        //第一种判断方式
        res = mysql_store_result(conn);
        //printf("mysql_num_rows = %lu\n", (unsigned long)mysql_num_rows(res));

        //第二种判断方式,两种方式不能一起使用
		/* res = mysql_use_result(conn); */
            
        row = mysql_fetch_row(res);
		if(NULL == row)
        {
            printf("Don't find any data\n");
        }
        else
		{
            do
            {	
				/* printf("num=%d\n",mysql_num_fields(res));//列数 */

                //每次for循环打印一整行的内容
				for(queryRet = 0; queryRet < mysql_num_fields(res); ++queryRet)
				{
                    if(!strcmp(row[queryRet],"0"))
                    {
                        //如果是普通文件返回0
                        return -1;
                    }
                    return 0;
                      // printf("%s", row[queryRet]);
				}

				printf("\n");

            }while(NULL != (row = mysql_fetch_row(res)));
		}

		mysql_free_result(res);
	}

	mysql_close(conn);

	return 0;
}
int compare(char* pwd,int fd)
{
	

	MYSQL *conn;
	MYSQL_RES *res;
	MYSQL_ROW row;
	char *server = "localhost";
	char *user = "chen";
	char *password = "123";
	char *database = "37chen";//要访问的数据库名称
	char query[300] = "select filename from file where owner_id='";

    strcat(query,pwd);
    strcat(query,"'");
    printf("query %s\n",query);
	unsigned int queryRet;
	//dup2(fd,STDOUT_FILENO);
	//strcpy(query,"select * from user"); 
    //printf("%s\n",query);
    
    //在输出前先打印查询语句
	//puts(query);

    //初始化
	conn = mysql_init(NULL);
    if(!conn)
    {
        printf("MySQL init failed\n");
        return -1;
    }

    //连接数据库，看连接是否成功，只有成功才能进行后面的操作
	if(!mysql_real_connect(conn, server, user, password, database, 0, NULL, 0))
	{
		printf("Error connecting to database: %s\n", mysql_error(conn));
		return -1;
	}
    else
    {
		//printf("MySQL Connected...\n");
	}

    //把SQL语句传递给MySQL
	queryRet = mysql_query(conn, query);
	if(queryRet)
	{
		printf("Error making query: %s\n", mysql_error(conn));
	}
    else
    {
    
        //用mysql_num_rows可以得到查询的结果集有几行
        //要配合mysql_store_result使用
        //第一种判断方式
        res = mysql_store_result(conn);
        //printf("mysql_num_rows = %lu\n", (unsigned long)mysql_num_rows(res));

        //第二种判断方式,两种方式不能一起使用
		/* res = mysql_use_result(conn); */
            
        row = mysql_fetch_row(res);
		if(NULL == row)
        {
            printf("Don't find any data\n");
        }
        else
		{
            dup2(fd,STDOUT_FILENO);
            do
            {	
				/* printf("num=%d\n",mysql_num_fields(res));//列数 */

                //每次for循环打印一整行的内容
                
				for(queryRet = 0; queryRet < mysql_num_fields(res); ++queryRet)
				{
                       
						printf("%s", row[queryRet]);
                        
				}

				printf("\n");

            }while(NULL != (row = mysql_fetch_row(res)));
		}

		mysql_free_result(res);
	}

	mysql_close(conn);

	return 0;
}
int cddata(int data,char* pwd)
{
    MYSQL *conn;
	MYSQL_RES *res;
	MYSQL_ROW row;
	char *server = "localhost";
	char *user = "chen";
	char *password = "123";
	char *database = "37chen";//要访问的数据库名称
	char query[300] = "select filename from file where owner_id=(select id from user where id='";
    char str[25];
    sprintf(str,"%d",data);//将传进来的用户Id转成字符串
    strcat(query,str);
    strcat(query,"')");
    strcat(query," and type=1");
	unsigned int queryRet;
   // printf("query %s\n",query);
	//printf("pwd = %s\n",pwd);
	//strcpy(query,"select * from user"); 

    //在输出前先打印查询语句
	//puts(query);

    //初始化
	conn = mysql_init(NULL);
    if(!conn)
    {
        printf("MySQL init failed");
        return -1;
    }

    //连接数据库，看连接是否成功，只有成功才能进行后面的操作
	if(!mysql_real_connect(conn, server, user, password, database, 0, NULL, 0))
	{
		printf("Error connecting to database: %s\n", mysql_error(conn));
		return -1;
	}
    else
    {
		//printf("MySQL Connected...\n");
	}

    //把SQL语句传递给MySQL
	queryRet = mysql_query(conn, query);
	if(queryRet)
	{
		printf("Error making query: %s\n", mysql_error(conn));
	}
    else
    {
    
        //用mysql_num_rows可以得到查询的结果集有几行
        //要配合mysql_store_result使用
        //第一种判断方式
        res = mysql_store_result(conn);
        //printf("mysql_num_rows = %lu\n", (unsigned long)mysql_num_rows(res));

        //第二种判断方式,两种方式不能一起使用
		/* res = mysql_use_result(conn); */
            
        row = mysql_fetch_row(res);
		if(NULL == row)
        {
            printf("Don't find any data\n");
        }
        else
		{
            do
            {	
				/* printf("num=%d\n",mysql_num_fields(res));//列数 */

                //每次for循环打印一整行的内容
                
				for(queryRet = 0; queryRet < mysql_num_fields(res); ++queryRet)
				{
                    int ret = 0;
                    if(ret = (cdjudge(row[queryRet])))
                    {
                        //普通文件
                        continue;
                    }
                    else if(!(strcmp(row[queryRet],pwd))&&ret ==0)
                    {
                        //如果是目录，就可以改变，否则就返回错误
                        update("user","pwd",pwd,str);
                        printf("找到该目录，可以成功进入%s\n",row[queryRet]);
                        break;
                    }
                   
					
				}

				printf("\n");

            }while(NULL != (row = mysql_fetch_row(res)));
		}

		mysql_free_result(res);
	}

	mysql_close(conn);

	return 0;
}

void cd(int fd,char* data,char *parent_id,char* userid)
{
    //文件描述符       cd 右边的参数 parent_id父节点需要改变
    //printf("main.c%s\n",data);
    
    //dup2(fd,1);
    //cddata(1,data);
    //cd需要修改当前工作目录，修改之前要判断数据库该用户有没有该目录
    //select filename from file where owner_id='2' and filename='arr2';
    char query[300]="select filename from file where owner_id='";
    strcat(query,userid);
    strcat(query,"' ");
    strcat(query,"and filename='");
    strcat(query,data);
    strcat(query,"'");
    strcat(query," and type=1");
    printf("客户端的查询命令是%s\n",query);
    if(selectcd(query,data)==1)
    {
        printf("客户端有此文件\n");
        //修改当前工作目录
        //update member set age = 15 where id = 1;
        char query[300]="update user set pwd='";
        strcat(query,data);
        strcat(query,"'");
        strcat(query,"where id='");
        strcat(query,userid);
        strcat(query,"'");
        printf("pwd = %s\n",query);
        updatePwd(query);
        //修改传进来的parent_id
        //char query1[300]="select id from file where owner_id=1 and parent_id='";
        //sprintf(query1,"%s%s' and type=1 and filename='%s'",query1,parent_id,data);
        char query1[300]="select id from file where owner_id='";
        sprintf(query1,"%s%s' and parent_id='%s' and type=1 and filename='%s'",query1,userid,parent_id,data);
        printf("update select = %s\n",query1);
        if(updateparent_id(query1)!=NULL)
        {
            strcpy(parent_id,updateparent_id(query1));
            char* sed="yes";
            send(fd,sed,strlen(sed),0); 
        }
    }
    else
    {
        char *arr="The client does not have the file, or it is a plain file";
        send(fd,arr,strlen(arr),0);
    }
    //先查数据库有没有这个filename
    
}
void ls(int fd,char *parent_id,char* userId)
{
    //文件描述符，父id，用户id
   char query[300]="select filename from file where owner_id='";
   //2 and parent_id=15 
   strcat(query,userId);
   strcat(query,"' and parent_id='");
   strcat(query,parent_id);
   strcat(query,"'");
   printf("ls 查询语句为:%s\n",query);
   selectls(query,fd);
    
}
int deleteMyfile(int fd,char* userid,char* filename)
{
    //还是先查找有没有这个文件
    char query[100]="select filename from file where owner_id='";
    strcat(query,userid);
    strcat(query,"' ");
    strcat(query,"and filename='");
    strcat(query,filename);
    strcat(query,"'");
    strcat(query," and type=0");
    printf("删除查询 %s\n",query);
    if(selectcd(query,filename)==1)
    {
        char query1[100]="update file set cnt=cnt-1 where owner_id='";
        strcat(query1,userid);
        strcat(query1,"'");
        strcat(query1,"and filename='");
        strcat(query1,filename);
        strcat(query1,"'");
        printf("query1 修改引用计数的值----------------%s\n",query1);
        if(moveCnt(query1)==1)
        {
            //查找引用计数为0的字段，删掉
            memset(query1,0,sizeof(query));
            strcpy(query1,"delete from file where cnt=0");
            if(deleteCnt(query1)==1)
            {
                printf("数据库已删除file文件\n");
                remove(filename);//调用该函数，删除
            }
           
        }
        else
        {
            char* temp="You don't have this file";
            send(fd,temp,strlen(temp),0);
        }
    }
    else
    {
        char *status="You don't have this file";
        send(fd,status,strlen(status),0);
    }

}
void pwd(char* userId,int fd,char* pwd)
{
    
    //查询user表
    printf("查询user 表找pwd\n");
    char query[300]="select pwd from user where id='";
    strcat(query,userId);
    strcat(query,"'");
    selectpwd(query,fd);

   
}
int selectStrcmp(char* userid,char* md5)
{

}











